package ru.spies.content;

import java.util.Collection;

import ru.spies.dbwork.DBQueries;
import ru.spies.dbwork.DBWork;
import ru.spies.entities.Importance;
import ru.spies.entities.Role;
import ru.spies.entities.Siteuser;
import ru.spies.profileClasses.RoleInfo;
import ru.spies.profileClasses.UserInfo;

public class DBFunctionalContent extends AbstractContent {
	
	
		
	private static final String TABLE_NAME="showUsers";
	private static final String NORMAL_COLUMNS[]={"Логин","Группа"};

	private static DBFunctionalContent statThis=null;
	
	private DBFunctionalContent() {
		
	}
	
	public static DBFunctionalContent getInstance(){
		if (statThis==null){
			statThis=new DBFunctionalContent();
		}
		return statThis;
	}
	
	public String getUserContent(){
		StringBuffer htmlContent = new StringBuffer("<p>Имя пользователя:</p><select name=\"oldName\" class=\"txtBox\">");
		Collection<Siteuser> siteusers = (Collection<Siteuser>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_SITEUSERS_FIND);
		for (Siteuser siteuser : siteusers) {
			if ((siteuser.getId().intValue()!=UserInfo.getInstance().getMe().getId().intValue())&&(!RoleInfo.isRoleAdmin(siteuser.getRole().getName()))){
				htmlContent.append(getComboOption(siteuser.getUsername(),siteuser.getUsername()));
			}
		}
		htmlContent.append("</select><input type=\"submit\" class=\"inFormBtn\" value=\"Выбрать этого пользователя\">");
		return htmlContent.toString();
	}
	
	public String getUpdateContent(Role userRole){
		StringBuffer htmlContent = new StringBuffer("<p>Новое имя пользователя:</p><input type=\"text\" name=\"newName\" class=\"txtBox\"><select name=\"newRole\" class=\"txtBox\">");
		Collection<Role> roles = (Collection<Role>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_ROLE_FIND);
		htmlContent.append(getComboOption(RoleInfo.getTrueRole(userRole.getName()),userRole.getName()));
		for (Role role : roles) {
			if ((!RoleInfo.isRoleGuest(role.getName()))&&(role.getId().intValue()!=userRole.getId().intValue())){
				htmlContent.append(getComboOption(RoleInfo.getTrueRole(role.getName()),role.getName()));
			}		
		}
		htmlContent.append("</select><input type=\"submit\" class=\"inFormBtn\" value=\"Обновить\">");
		return htmlContent.toString();
	}
	
	
	
	public String getShowUsersContent(){
		return getDataTableContent(TABLE_NAME, getCols());
	}
	
	public String getSPContent(String paramHeader){
		return "<p>"+paramHeader+"</p><input name=\"paramName\" class=\"txtBox\" type=\"text\"><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Сделать запрос\">";
	}
	
	public String getImportanceContent(){
		StringBuffer htmlContent = new StringBuffer("<p>Выбрать тип важности:</p><select class=\"txtBox\" name=\"paramName\">");
		Collection<Importance> importances = (Collection<Importance>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_IMPORTANCE_FIND);
		if (importances.size()==0){
			return IMPORTANCE_ERROR;
		}
		for (Importance importance : importances) {
			htmlContent.append(getComboOption(importance.getType(), importance.getId().toString()));
		}
		htmlContent.append("</select><input type=\"submit\" name=\"send\" class=\"inFormBtn\" value=\"Использовать этот тип важности\">");
		return htmlContent.toString();
	}
	
	private String[] getCols(){
		String retVal[];
		if (UserInfo.getInstance().getPrivileges().canDoNormalShow()){
			retVal = new String[NORMAL_COLUMNS.length+1];
			retVal[NORMAL_COLUMNS.length]="Имя агента";
		} else {
			retVal = new String[NORMAL_COLUMNS.length];
		}
		for (int i = 0; i < NORMAL_COLUMNS.length; i++) {
			retVal[i]=NORMAL_COLUMNS[i];
		}
		return retVal;
	}
	
	public String getSPTableContent(String tableName, String[] cols){
		return getDataTableContent(tableName, cols);
	}
	
	public String getGoodOrBadCountry(boolean good){
		String exists;
		if (good){
			exists=" ";
		} else {
			exists=" не ";
		}
		return "<p>Такая страна"+exists+"существует<p>"+RETURN_HOME;
	}
	
}
